import abc

from .task_running import RunningTask, RunningTaskPlugin


class SteppingTaskPlugin(RunningTaskPlugin, abc.ABC):
    def on_before_step(self):
        pass

    def on_after_step(self):
        pass


class SteppingTask(RunningTask, abc.ABC):
    def _run(self):
        while True:
            try:
                self._before_step()
                self._step()
                self._after_step()
            except StopIteration:
                break

    def _before_step(self):
        self.dock.scan_call_plugins(
            self.dock.iter_plugins(SteppingTaskPlugin),
            lambda plugin: plugin.on_before_step())

    @abc.abstractmethod
    def _step(self):
        pass

    def _after_step(self):
        self.dock.scan_call_plugins(
            self.dock.iter_plugins(SteppingTaskPlugin),
            lambda plugin: plugin.on_after_step())
